package com.googlecode.derby.customize;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;

import com.googlecode.derby.customize.exception.DbManagerException;

/**
 * @author o.tanaka
 */
public final class DbManager {

    /**
     * DbManager SHUTDOWN.
     */
    public static final String SHUTDOWN = "shutdown";
    /**
     * DbManager CREATE.
     */
    public static final String CREATE = "create";

    /**
     * @param dbname
     *            database name
     * @param attribute
     *            create/shutdown
     * @param key
     *            key
     * @return connection
     *         DriverManager.getConnectionのエラー
     */
    public static Connection getConnection(
            final String dbname,
            final String attribute,
            final String key) {
        final String url = DbManager.getUrl(dbname, attribute, key);
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url);
        } catch (final SQLException e) {
            final String sqlstate = e.getSQLState();
            if (!"XJ015".equals(sqlstate) && !"08006".equals(sqlstate)) {
                throw new DbManagerException("getConnection:", e);
            }
        }
        return conn;
    }

    /**
     * @param dbname
     *            database name
     * @param attribute
     *            create/shutdown
     * @param user
     *            user
     * @param password
     *            password
     * @param key
     *            encryptionKey
     * @return connection
     */
    public static Connection getConnection(
            final String dbname,
            final String attribute,
            final String user,
            final String password,
            final String key) {
        final String url = DbManager.getUrl(dbname, attribute, key);
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (final SQLException e) {
            throw new DbManagerException("getConnection:", e);
        }
    }

    /**
     * @param dbname
     * @param attribute
     * @return url
     * @throws ClassNotFoundException
     */
    private static String getUrl(
            final String dbname,
            final String attribute,
            final String key) {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        } catch (final ClassNotFoundException e) {
            throw new DbManagerException("getUrl:", e);
        }
        final StringBuilder url = new StringBuilder("jdbc:derby:");
        url.append(dbname);
        url.append(";").append(attribute).append("=true");
        if (StringUtils.isNotEmpty(key)) {
            url.append(";dataEncryption=true");
            url.append(";encryptionAlgorithm=DES/CBC/NoPadding");
            url.append(";encryptionKey=");
            url.append(key);
        }
        return url.toString();
    }

    private DbManager() {
        super();
    }

}
